public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode right;
        public TreeNode left;

        public TreeNode(char val) {
            this.val = val;
        }

    }
//        穷举 --- 创建二叉树
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;
        return A;
    }

//    前序遍历
    public void preOrder(TreeNode root){
        if (root == null){
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

//    中序遍历
    public void inOrder(TreeNode root){
        if (root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

//    后续遍历
    public void postOrder(TreeNode root){
        if (root==null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

//    计算节点个数
    public static int nodeSize;
    public void size(TreeNode root){
        if (root == null){
            return;
        }
        nodeSize++;
        size(root.right);
        size(root.left);
    }

//    获取树中节点的个数  左子树节点+右子树节点+根节点
    public int size2(TreeNode root){
        if (root == null){
            return 0;
        }
        int tmp = size2(root.left)+size2(root.right)+1;
        return tmp;
    }

//    获取叶子结点个数
    public int leaf;
    public void getLeafNodeCount1(TreeNode root){
        if (root == null){
            return;
        }
        if (root.left == null &&root.right == null){
            leaf++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

    public int getLeafNodeCount2(TreeNode root){
        if (root == null){
            return 0;
        }
        if (root.left == null &&root.right == null){
            return 1;
        }
        return getLeafNodeCount2(root.left)+getLeafNodeCount2(root.right);
    }

//    计算第k层的节点个数
    public int getLevelNodeCount(TreeNode root,int k){
        if (root==null){
            return 0;
        }
        if (k == 1){
            return 1;
        }
        return getLevelNodeCount(root.left,k-1)+getLevelNodeCount(root.right,k-1);
    }

//    计算二叉树高度 = 左子树的高度和右子树的最大值+1
    public int getHeight(TreeNode root){
        if (root == null){
            return 0;
        }
        return Math.max(getHeight(root.left),getHeight(root.right))+1;
    }

//  检测值为value的元素是否存在
    public TreeNode find(TreeNode root,int val){
        if (root == null){
            return null;
        }
        if (root.val == val){
            return root;
        }
        TreeNode leftValue = find(root.left,val);
        if (leftValue != null){
            return leftValue;
        }
        TreeNode rightValue = find(root.right,val);
        if (rightValue != null){
            return rightValue;
        }
        return null;
    }

    public int hight(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftH = hight(root.left);
        int rightH = hight(root.right);
        return Math.max(leftH,rightH)+1;
    }

    public boolean isBalance(TreeNode root){
        if (root == null){
            return true;
        }
        int leftH = hight(root.left);
        int right = hight(root.right);
        if (Math.abs(leftH-right) < 2 && isBalance(root.left) && isBalance(root.right)){
            return true;
        }
        return false;
    }

    public void text(){
        Object object = new Object();
    }
}
